package net.alche.fa.core.doing.invoker;
import net.alche.fa.core.doing.FaAnnoDoing;
import net.alche.fa.core.model.FaValue;
import java.util.List;
import net.alche.fa.core.FaException;
import net.alche.al.util.AC;

// FaListInvoker
@FaAnnoDoing 
@SuppressWarnings("all") 
public class FaListInvoker  extends AbstractObjectInvoker  {

  public FaListInvoker () {}

  // 是否为此对象
  public boolean  matched(FaValue  objVal, Object  obj, String  methodName, List<FaValue> params) {
    if ( obj instanceof List ) {
      return true;
    }
    return false;
  }

  // 调用
  public FaValue  invoke(FaValue  objVal, Object  obj, String  methodName, List<FaValue> params) throws FaException  {
    List  list = (List )obj;
    FaValue  first = null;
    if ( params != null && !params.isEmpty()) {
      first = params.get(0);
    }
    switch ( methodName) {
      case "isEmpty" : {
        return new FaValue (list.isEmpty());
      }
      case "get" : {
        return new FaValue (list.get(first.getIntValue().intValue()));
      }
      case "add" : {
        return new FaValue (list.add(first.getValue()));
      }
      case "size" : {
        return new FaValue (list.size());
      }
      case "remove" : {
        if ( first.getType() == FaValue ._INT) {
          return new FaValue (list.remove(first.getIntValue().intValue()));
        }
        else {
          return new FaValue (list.remove(first));
        }
      }
      case "addAll" : {
        if ( first.getType() == FaValue ._INT) {
          int  index = first.getIntValue().intValue();
          FaValue  val = params.get(2);
          return new FaValue (list.addAll(index, (List )val.getValue()));
        }
        else {
          return new FaValue (list.addAll((List )first.getValue()));
        }
      }
      default : {
        throw new FaException (FaException .LESS_METHOD, "fa-list method not find | method = " + methodName, "params=" + AC .json.toStr(params));
      }
    }
  }
}